<?php
namespace Qgs\Controller;

class IndexController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
    }

    public function index()
    {
        //待回复咨询 待发货订单 待确认结算 库存缺货 待处理退款
        //品类销售分析=>销售top5 销售额分析14天 订单分析10天(下单数量/退单数量)
        $daihuifu = M('b2c_order_complain')->where(['local_id'=>$this->local_id, 'member_id' => ['gt', 0], 'pid' => 0, 'is_reply'=>'false'])->count();
        //待发货订单
        $time = time();
        $daifahuo_where = [
            'o.pay_status' => '1',
            'o.ship_status' => ['IN', ['0', '2']],
            'o.status' => 'active',
            'o.local_id' => $this->local_id,
        ];
        $daifahuo = M('b2c_orders')
            ->alias('o')
            ->join(C('DB_PREFIX') . 'pam_members m on  o.member_id = m.member_id', 'LEFT')
            ->where($daifahuo_where)
            ->count();
        //待确认结算
        $jiesuan_where = [
            'local_id' => $this->local_id,
            'state' => '0',
        ];
        $jiesuan = M('b2c_bills_account')->where($jiesuan_where)->count();
        //库存缺货
        $kucun_where = [
            'local_id' => $this->local_id,
            'store - freez' => ['LT', 1],
        ];
        $kucun = M('b2c_store_product')->where(['local_id' => $this->local_id])->where('`store` <= `freez` or `store` <= 0')->count();
        //待处理退款
        $tuikuan_where = [
            'b.local_id' => $this->local_id,
            'a.status' => '1',
        ];
        $tuikuan = M('b2c_refund_money')->alias('a')->join(C('DB_PREFIX') . 'b2c_orders b on a.order_id=b.order_id')->where($tuikuan_where)->count();

        $this->assign([
            'daihuifu' => $daihuifu,
            'daifahuo' => $daifahuo,
            'jiesuan' => $jiesuan,
            'kucun' => $kucun,
            'tuikuan' => $tuikuan,
        ]);
        //销售top5
        /*data: [
         {label: "食品", value: 112},
         {label: "日用", value: 30},
         {label: "蔬菜", value: 20}
         ],*/
        $saletop = $this->saleTopList();

        //按天计算14天内的销售额
        /*data: [
             {y: '2011 Q1', item1: 2666},
             {y: '2011 Q2', item1: 2778},
             {y: '2011 Q3', item1: 4912},
             {y: '2011 Q4', item1: 3767},
             {y: '2012 Q1', item1: 6810},
             {y: '2012 Q2', item1: 5670},
             {y: '2012 Q3', item1: 4820},
             {y: '2012 Q4', item1: 15073},
             {y: '2013 Q1', item1: 10687},
             {y: '2013 Q2', item1: 8432}
             ],*/
        $xiaoshoulist = $this->xiaoshoulist();

        //订单分析10天(下单数量/退单数量)
        /*data: [
                {y: '1月10', a: 100, b: 90},
                {y: '1月11', a: 75, b: 65},
                {y: '1月12', a: 50, b: 40},
                {y: '1月13', a: 75, b: 65},
                {y: '1月14', a: 50, b: 40},
                {y: '1月15', a: 75, b: 65},
                {y: '1月16', a: 100, b: 90},
                {y: '1月17', a: 80, b: 60},
                {y: '1月18', a: 70, b: 30},
                {y: '1月19', a: 90, b: 40},
                {y: '1月20', a: 120, b: 50}
            ],*/
        $dingdanfenxi = $this->dingdanfenxi();

        $this->assign([
            'saletop' => json_encode($saletop),
            'xiaoshoulist' => json_encode($xiaoshoulist),
            'dingdanfenxi' => json_encode($dingdanfenxi),
        ]);
        $this->display();
    }

    public function getDate($count=10)
    {
        $today = strtotime("-1 day");
        $before = $today - ($count * 86400);
        $list = [];
        for ($i=0; $i<$count; $i++) {
            array_unshift($list, date("Y-m-d", $today - ($i * 86400)));
        }
        $res = ['before' => date("Y-m-d", $before), 'today' => date("Y-m-d", $today), 'list'=> $list];
        return $res;
    }

    //按天计算14天内的销售额
    public function xiaoshoulist() {
        $res = $this->getDate(42);
        $xiaoshoulist = M()->query("
        SELECT FROM_UNIXTIME( o.`time`,'%Y-%m-%d') AS order_day,
        sum(o.`money`) AS moneys 
        FROM ".C('DB_PREFIX')."b2c_bills AS o WHERE o.`local_id`=".$this->local_id." AND o.`bill_type` = 'payments'
        GROUP BY order_day HAVING order_day > '".$res['before']."' AND order_day <= '".$res['today']."';
        ");
        $list = [];
        if (count($res['list']) > 0) {
            $day = array_column($xiaoshoulist, 'order_day');
            $money = array_column($xiaoshoulist, 'moneys');
            foreach ($res['list'] as $key => $item) {
                if ($xiaoshoulist) {
                    if (in_array($item, $day)) {
                        $list[$key] = ['order_day'=>$item, 'moneys'=>$money[array_keys($day, $item)[0]]];
                    } else {
                        $list[$key] = ['order_day'=>$item, 'moneys'=>0];
                    }
                } else {
                    $list[$key] = ['order_day'=>$item, 'moneys'=> 0];
                }
            }
        }
        return $list;
    }

    /**得到销售top前五的商品
     * @return array
     */
    public function saleTopList() {
        $list = [];
        $list = M('b2c_bills')
            ->alias('a')
            ->join(C('DB_PREFIX').'b2c_order_items b on a.order_id=b.order_id')
            ->field('b.`name` as label,SUM(b.`nums`) as value')
            ->where("a.local_id={$this->local_id}")
            ->group('label')
            ->order('value desc')
            ->limit(5)
            ->select();
        return $list;
    }

    //订单分析10天(下单数量/退单数量)
    public function dingdanfenxi() {
        $res = $this->getDate(10);
        //支付列表
        $pay = M()->query("
        SELECT FROM_UNIXTIME( o.`time`,'%Y-%m-%d') AS order_day,
        count(o.`id`) AS pays 
        FROM ".C('DB_PREFIX')."b2c_bills AS o WHERE o.`local_id`=".$this->local_id." AND o.`bill_type` = 'payments'
        GROUP BY order_day HAVING order_day > '".$res['before']."' AND order_day <= '".$res['today']."';
        ");
        //退单列表
        $ref = M()->query("
        SELECT FROM_UNIXTIME( o.`time`,'%Y-%m-%d') AS order_day,
        count(o.`id`) AS refunds 
        FROM ".C('DB_PREFIX')."b2c_bills AS o WHERE o.`local_id`=".$this->local_id." AND o.`bill_type` = 'refunds'
        GROUP BY order_day HAVING order_day > '".$res['before']."' AND order_day <= '".$res['today']."';
        ");
        $list = [];
        if (count($res['list']) > 0) {
            $day = array_column($pay, 'order_day');
            $pays = array_column($pay, 'pays');

            $day2 = array_column($ref, 'order_day');
            $refunds = array_column($ref, 'refunds');
            foreach ($res['list'] as $key => $item) {
                $temp = [];
                if($pay) {
                    if (in_array($item, $day)) {
                        $temp = array_merge($temp, ['order_day'=>$item, 'pays'=>$pays[array_keys($day, $item)[0]]]);
                    } else {
                        $temp = array_merge($temp, ['order_day'=>$item, 'pays'=>0]);
                    }
                } else {
                    $temp = array_merge($temp, ['order_day'=>$item, 'pays'=>0]);
                }
                if($ref) {
                    if (in_array($item, $day2)) {
                        $temp = array_merge($temp, ['order_day'=>$item, 'refunds'=>$refunds[array_keys($day2, $item)[0]]]);
                    } else {
                        $temp = array_merge($temp, ['order_day'=>$item, 'refunds'=>0]);
                    }
                } else {
                    $temp = array_merge($temp, ['order_day'=>$item, 'refunds'=>0]);
                }
                $list[$key] = $temp;
            }
        }
        //dump($list);
        return $list;
    }
}